package com.zzh.lib.views.drawer;


import android.graphics.Canvas;
import android.graphics.RectF;

import com.zzh.lib.views.banner.IndicatorOptions;
import com.zzh.lib.views.def.IndicatorSlideMode;
import com.zzh.lib.views.utils.BannerUtils;

/**
 * Created by ZZH on 2023/2/16.
 *
 * @date: 2023/2/16 11:30
 * @email: zzh_hz@126.com
 * @author: zzh
 * @description: com.zzh.lib.views.drawer
 */
public class CircleDrawer extends BaseDrawer {
    private RectF rectF = new RectF();

    @Override
    protected int measureHeight() {
        return (int) (maxWidth + INDICATOR_PADDING_ADDITION);
    }

    public CircleDrawer(IndicatorOptions indicatorOptions) {
        super(indicatorOptions);
    }

    @Override
    public void onDraw(Canvas canvas) {
        int pageSize = mIndicatorOptions.pageSize;
        if (pageSize > 1 || mIndicatorOptions.showIndicatorOneItem && pageSize == 1) {
            drawNormal(canvas);
            drawSlider(canvas);
        }
    }

    private void drawNormal(Canvas canvas) {
        float normalIndicatorWidth = mIndicatorOptions.normalSliderWidth;
        mPaint.setColor(mIndicatorOptions.normalSliderColor);
        for (int i = 0; i < mIndicatorOptions.pageSize; i++) {
            float coordinateX = BannerUtils.getCoordinateX(mIndicatorOptions, maxWidth, i);
            float coordinateY = BannerUtils.getCoordinateY(maxWidth);
            drawCircle(canvas, coordinateX, coordinateY, normalIndicatorWidth / 2);
        }
    }

    private void drawSlider(Canvas canvas) {
        mPaint.setColor(mIndicatorOptions.checkedSliderColor);
        switch (mIndicatorOptions.slideMode) {
            case IndicatorSlideMode.NORMAL:
            case IndicatorSlideMode.SMOOTH:
                drawCircleSlider(canvas);
                break;
            case IndicatorSlideMode.WORM:
                drawWormSlider(canvas);
                break;
            case IndicatorSlideMode.SCALE:
                drawScaleSlider(canvas);
                break;
            case IndicatorSlideMode.COLOR:
                drawColor(canvas);
                break;
        }
    }

    private void drawColor(Canvas canvas) {
        int currentPosition = mIndicatorOptions.currentPosition;
        float slideProgress = mIndicatorOptions.slideProgress;
        float coordinateX = BannerUtils.getCoordinateX(mIndicatorOptions, maxWidth, currentPosition);
        float coordinateY = BannerUtils.getCoordinateY(maxWidth);
        float evaluate = (float) argbEvaluator.evaluate(
                slideProgress, mIndicatorOptions.checkedSliderColor, mIndicatorOptions.normalSliderColor
        );
        mPaint.setColor((int) evaluate);
        drawCircle(canvas, coordinateX, coordinateY, mIndicatorOptions.normalSliderWidth / 2);

        // 绘制可循环的ViewPager指示器渐变
        evaluate = (float) argbEvaluator.evaluate(
                1 - slideProgress, mIndicatorOptions.checkedSliderColor, mIndicatorOptions.normalSliderColor
        );
        mPaint.setColor((int) evaluate);
        float nextCoordinateX;
        if (currentPosition == mIndicatorOptions.pageSize - 1) {
            nextCoordinateX = BannerUtils.getCoordinateX(mIndicatorOptions, maxWidth, 0);
        } else {
            nextCoordinateX = coordinateX + mIndicatorOptions.sliderGap + mIndicatorOptions.normalSliderWidth;
        }
        drawCircle(canvas, nextCoordinateX, coordinateY, mIndicatorOptions.checkedSliderWidth / 2);
    }

    private void drawScaleSlider(Canvas canvas) {
        int currentPosition = mIndicatorOptions.currentPosition;
        float slideProgress = mIndicatorOptions.slideProgress;
        float coordinateX = BannerUtils.getCoordinateX(mIndicatorOptions, maxWidth, currentPosition);
        float coordinateY = BannerUtils.getCoordinateY(maxWidth);
        if (slideProgress < 1) {
            float evaluate = (float) argbEvaluator.evaluate(
                    slideProgress, mIndicatorOptions.checkedSliderColor, mIndicatorOptions.normalSliderColor
            );
            mPaint.setColor((int) evaluate);
            float radius =
                    mIndicatorOptions.checkedSliderWidth / 2 - (mIndicatorOptions.checkedSliderWidth / 2 - mIndicatorOptions.normalSliderWidth / 2) * slideProgress;
            drawCircle(canvas, coordinateX, coordinateY, radius);
        }

        if (currentPosition == mIndicatorOptions.pageSize - 1) {
            float evaluate = (float) argbEvaluator.evaluate(
                    slideProgress, mIndicatorOptions.normalSliderColor, mIndicatorOptions.checkedSliderColor
            );
            mPaint.setColor((int) evaluate);
            float nextCoordinateX = maxWidth / 2;
            float nextRadius = minWidth / 2 + (maxWidth / 2 - minWidth / 2) * (slideProgress);
            drawCircle(canvas, nextCoordinateX, coordinateY, nextRadius);
        } else {
            if (slideProgress > 0) {
                float evaluate = (float) argbEvaluator.evaluate(
                        slideProgress, mIndicatorOptions.normalSliderColor, mIndicatorOptions.checkedSliderColor
                );
                mPaint.setColor((int) evaluate);
                float nextCoordinateX =
                        coordinateX + mIndicatorOptions.sliderGap + mIndicatorOptions.normalSliderWidth;
                float nextRadius =
                        mIndicatorOptions.normalSliderWidth / 2 + (mIndicatorOptions.checkedSliderWidth / 2 - mIndicatorOptions.normalSliderWidth / 2) * slideProgress;
                drawCircle(canvas, nextCoordinateX, coordinateY, nextRadius);
            }
        }
    }

    private void drawCircleSlider(Canvas canvas) {
        int currentPosition = mIndicatorOptions.currentPosition;
        float startCoordinateX =
                BannerUtils.getCoordinateX(mIndicatorOptions, maxWidth, currentPosition);
        float endCoordinateX = BannerUtils.getCoordinateX(
                mIndicatorOptions, maxWidth, (currentPosition + 1) % mIndicatorOptions.pageSize
        );
        float coordinateX =
                startCoordinateX + (endCoordinateX - startCoordinateX) * mIndicatorOptions.slideProgress;
        float coordinateY = BannerUtils.getCoordinateY(maxWidth);
        float radius = mIndicatorOptions.checkedSliderWidth / 2;
        drawCircle(canvas, coordinateX, coordinateY, radius);
    }

    private void drawWormSlider(Canvas canvas) {
        float sliderHeight = mIndicatorOptions.normalSliderWidth;
        float slideProgress = mIndicatorOptions.slideProgress;
        int currentPosition = mIndicatorOptions.currentPosition;
        float distance = mIndicatorOptions.sliderGap + mIndicatorOptions.normalSliderWidth;
        float startCoordinateX =
                BannerUtils.getCoordinateX(mIndicatorOptions, maxWidth, currentPosition);

        float v = distance * (slideProgress - 0.5f) * 2.0f;
        if (v < 0f) {
            v = 0f;
        }
        float left = startCoordinateX + v - mIndicatorOptions.normalSliderWidth / 2 + INDICATOR_PADDING;

        float v1 = distance * slideProgress * 2f;
        if (v1 > distance) {
            v1 = distance;
        }


        float right = startCoordinateX + v1 + mIndicatorOptions.normalSliderWidth / 2 + INDICATOR_PADDING;
        rectF.set(left, (float) INDICATOR_PADDING, right, sliderHeight + INDICATOR_PADDING);
        canvas.drawRoundRect(rectF, sliderHeight, sliderHeight, mPaint);
    }

    private void drawCircle(
            Canvas canvas,
            float coordinateX,
            float coordinateY,
            float radius
    ) {
        canvas.drawCircle(
                coordinateX + INDICATOR_PADDING, coordinateY + INDICATOR_PADDING, radius, mPaint
        );
    }
}
